package cn.wd.common.vo;

import cn.wd.common.enums.ResponseEnum;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author wangdong
 * @date 2024/9/12 10:32
 **/
@Data
@Accessors(chain = true)
public class R<T> {

    private Integer code;
    private String msg;
    private T data;

    public static <T> R<T> OK(T data) {
        R<T> base = baseOk();
        return base.setData(data);
    }



    public static <T> R<T> OK() {
        return baseOk();
    }

    public static <T> R<T> OK(String msg) {
        return new R<T>()
                .setCode(ResponseEnum.SUCCESS.getCode())
                .setMsg(msg);
    }

    public static <T> R<T> OK(String msg, T data) {
        return new R<T>()
                .setCode(ResponseEnum.SUCCESS.getCode())
                .setMsg(msg)
                .setData(data);
    }

    public static <T> R<T> FAIL() {
        return FAIL(ResponseEnum.FAIL.getMsg());
    }

    public static <T> R<T> FAIL(String msg) {
        return new R<T>()
                .setCode(ResponseEnum.FAIL.getCode())
                .setMsg(msg);
    }

    public static <T> R<T> FAIL(ResponseEnum response) {
        return new R<T>()
                .setCode(response.getCode())
                .setMsg(response.getMsg());
    }

    public static <T> R<T> FAIL(Integer code, String msg) {
        return new R<T>().setCode(code).setMsg(msg);
    }

    private static  <T> R<T> baseOk() {
        return new R<T>()
                .setCode(ResponseEnum.SUCCESS.getCode())
                .setMsg(ResponseEnum.SUCCESS.getMsg());
    }
}
